package com.android.model.upload;

import android.util.Log;

import com.android.bean.Good;
import com.android.bean.ImageBean;
import com.android.bean.ResponseData;
import com.android.utils.constraint.Constants;

import java.io.File;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MultipartBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class UpModelimpl implements UpModel{

    @Override
    public void UpImage(List<MultipartBody.Part> parts,final UpListener upListener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();

        UpHttp_interface anInterface = retrofit.create(UpHttp_interface.class);
        Observable<ResponseData<ImageBean>> observable = anInterface.sendUpImage(parts);

        observable.observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<ImageBean>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {


                    }

                    @Override
                    public void onNext(@NonNull ResponseData<ImageBean> listResponseData) {
                        upListener.upImageOnSuccess(listResponseData);
                        Log.e("图片返回",listResponseData.getMsg());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("onerror",e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    public void addGoods(Good good, UpListener upListener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build();

        UpHttp_interface httpInterface = retrofit.create(UpHttp_interface.class);
        Observable<ResponseData<Good>> observable = httpInterface.sendAddGoods(good);

        observable.observeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData<Good>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseData<Good> goodResponseData) {
                        Log.e("发布商品",goodResponseData.getMsg());
                        upListener.addGoodsOnSuccess(goodResponseData);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("1",e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
